package com.simtop.Ds;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.simtop.service.KsServer;
import com.simtop.vo.ksvo.Ksvo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * @ClassName: StuJob
 * @Description TODO
 * @Author liuzhaobo
 * @Date 2022/11/9 0009 8:46
 * @Version 1.0
 **/
@RestController
@RequestMapping("/StuJob")
@CrossOrigin
@Component
@EnableScheduling
public class StuJob {

    @Autowired
    private KsServer ksServer;

    /**
     * @Author: liuzhaobo
     * @Description: 自动提交
     * @DateTime: 2022/11/9 0009 8:48
     * @Params:
     * @Return
     */
    @Bean
    public TaskScheduler taskScheduler() {
        ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
        taskScheduler.setPoolSize(64);
        return taskScheduler;
    }


    @Scheduled(cron = "*/1 * * * * ?")
    public void scheduledRusterss() {

        List<Ksvo> allList = ksServer.findAllList();
        for (Ksvo as : allList) {
            Integer id = as.getId();
            Date endTime = as.getEndTime();
            Date date = new Date();
            LocalDateTime begins = LocalDateTimeUtil.of(date);
            LocalDateTime ends = LocalDateTimeUtil.of(endTime);
            Duration between = LocalDateTimeUtil.between(begins, ends);
            long l = between.toMillis();
            if (l <= 0) {
                //说明 超时 自动修改状态
                ksServer.updateStatus(id, endTime);
            }
        }

    }


}
